package com.bnz.shop.service.impl;

import com.bnz.shop.dao.*;
import com.bnz.shop.entity.GoodsDescEntity;
import com.bnz.shop.entity.ItemEntity;
import com.bnz.shop.entity.group.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;

import com.bnz.shop.dao.GoodsDao;
import com.bnz.shop.entity.GoodsEntity;
import com.bnz.shop.service.GoodsService;
import org.springframework.transaction.annotation.Transactional;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    private GoodsDescDao goodsDescDao;
    @Autowired
    private ItemCatDao itemCatDao;
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private SellerDao sellerDao;
    @Autowired
    private ItemDao itemDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 添加商品--
     * 需要事务--
     * @param goods
     */
    @Transactional
    @Override
    public void save(Goods goods) {
        //1,添加spu商品 tb_goods
        goods.getGoods().setSellerId("bng");
        this.save(goods.getGoods());

        //2,添加spu商品描述tb_goodsDesc
        goods.getGoodsDesc().setGoodsId(goods.getGoods().getId());
        goodsDescDao.insert(goods.getGoodsDesc());
        //3,添加sku商品列表
        for (ItemEntity itemEntity : goods.getItemList()) {
            addItem(goods,itemEntity);
        }
    }
    //添加sku商品
    private void addItem(Goods goods, ItemEntity itemEntity) {
        // 1，绑定外键
        itemEntity.setGoodsId(goods.getGoods().getId());
        //2,s设置分类id
        itemEntity.setCategoryid((goods.getGoods().getCategory3Id()));
        //3，设置分类名称
        String id =goods.getGoods().getCategory3Id();
        String name =itemCatDao.selectById(id).getName();
        itemEntity.setCategory(name);
        //4,设置品牌名称
        String brandId =goods.getGoods().getBrandId();
        String brandName = brandDao.selectById(brandId).getName();
        itemEntity.setBrand(brandName);
        //5,设置商家名称
        String sellerId = goods.getGoods().getSellerId();
        String sellerName = sellerDao.selectById(sellerId).getName();
        itemEntity.setSeller(sellerName);
        //6,设置创建时间与修改时间
        itemEntity.setCreateTime(new Date());
        itemEntity.setUpdateTime(new Date());
        //7, 设置标题
        itemEntity.setTitle(goods.getGoods().getGoodsName() + "-标题");
        //8,设置商家名字
        itemEntity.setSellerId("bnz");
        //9,添加sku商品
        itemDao.insert(itemEntity);
    }
    @Override
    public Goods findById(String id) {
        //查询商品信息
        GoodsEntity byId = this.getById(id);
        //查询商品描述表的内容
        GoodsDescEntity goodsDescEntity = goodsDescDao.selectById(id);
        //查询sku商品列表内容
        List<ItemEntity> itemList = itemDao.selectList(new QueryWrapper<ItemEntity>().eq("goods_id", id));
        //搞出构造方法
        return new Goods(byId,goodsDescEntity,itemList);
    }

    //一对多时，多对多时，先删除多的，在添加新的
    @Override
    @Transactional
    public void update(Goods goods) {
        //修改商品表
        this.updateById(goods.getGoods());
        //修改desc表
        goodsDescDao.updateById(goods.getGoodsDesc());
        //修改sku商品列表
        //根据goodsiId先删除Item表中的数据，在添加数据
        //进行删除
        itemDao.delete(new QueryWrapper<ItemEntity>().eq("goods_id",goods.getGoods().getId()));
        // 进行添加
        for (ItemEntity itemEntity : goods.getItemList()) {
            addItem(goods,itemEntity);
        }

    }

}